﻿Imports SFDL.Crypto
Imports SFDL.Loader.My.Resources
Imports System.Globalization
Imports System.Threading

Public Class frm_opensfdl_encrypted

    Private _password As String
    Private _test_decrypt_string As String
    Private _sfdl_file_name As String

    Protected Friend ReadOnly Property GetPassword As String
        Get
            Return _password
        End Get
    End Property

    Protected Friend WriteOnly Property SetTestDecryptString As String
        Set(value As String)
            _test_decrypt_string = value
        End Set
    End Property

    Protected Friend WriteOnly Property SetSFDLFileName As String
        Set(value As String)
            _sfdl_file_name = value
        End Set
    End Property

    Private Sub OK_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OK.Click

        Dim _crypto As New AES
        Dim _tmp As String

        _password = PasswordTextBox.Text

        Try

            _tmp = _crypto.DecryptString(_test_decrypt_string, _password)

            Me.DialogResult = DialogResult.OK
            Me.Close()

        Catch ex As FalsePasswordException

            ToolTipPassword.IsBalloon = True
            ToolTipPassword.UseAnimation = True
            ToolTipPassword.UseFading = True
            ToolTipPassword.SetToolTip(PasswordTextBox, StringMessages.OpenSFDLEncryptetToolTipMessage)
            ToolTipPassword.ToolTipTitle = StringMessages.OpenSFDLEncryptetToolTipTitle
            ToolTipPassword.ToolTipIcon = ToolTipIcon.Error

            ToolTipPassword.Active = True
            ToolTipPassword.Show(StringMessages.OpenSFDLEncryptetToolTipMessage, PasswordTextBox)

            PasswordTextBox.Clear()

        End Try

    End Sub

    Private Sub Cancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Cancel.Click
        Me.DialogResult = DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub LoginForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        SetText(String.Format(StringMessages.OpenSFDLEncryptetPrompt.ToString, _sfdl_file_name))

    End Sub

    Private Sub PasswordTextBox_KeyDown(sender As Object, e As KeyEventArgs) Handles PasswordTextBox.KeyDown
        If e.KeyCode = Keys.Return Or e.KeyCode = Keys.Enter Then
            OK.PerformClick()
        End If
    End Sub

    Private Sub SetText(ByVal _text As String)

        If Me.InvokeRequired Then
            Me.Invoke(Sub() Me.SetText(_text))
        Else
            Label1.Text = _text
        End If

    End Sub

    Public Sub New()

        Thread.CurrentThread.CurrentUICulture = Globalization.CultureInfo.GetCultureInfoByIetfLanguageTag(GetSettings.Language)

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub
End Class